/*
 * SteinharHartModel.cpp
 *
 * Copyright 2012 Pieter Agten
 *
 * This file is part of tltgen.
 *
 * tltgen is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * tltgen is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with tltgen.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file   SteinhartHartModelThermistor.cpp
 * @author Pieter Agten (pieter.agten@gmail.com)
 * @date   2 sep 2012
 */


#include <boost/lexical_cast.hpp>
#include <cmath>
#include <stdexcept>

#include "ThermistorVisitor.hpp"

#include "SteinhartHartModelThermistor.hpp"

namespace tltgen {

  SteinhartHartModelThermistor::SteinhartHartModelThermistor(double a, double b, double c) :
    a(a), b(b), c(c)
  {
    if (! isValidA(a)) {
      throw std::invalid_argument("Value '" + boost::lexical_cast<std::string>(a) + "' is invalid for parameter a.");
    }
    if (! isValidB(b)) {
      throw std::invalid_argument("Value '" + boost::lexical_cast<std::string>(b) + "' is invalid for parameter b.");
    }
    if (! isValidC(c)) {
      throw std::invalid_argument("Value '" + boost::lexical_cast<std::string>(c) + "' is invalid for parameter c.");
    }
  }

  SteinhartHartModelThermistor::~SteinhartHartModelThermistor() { };

  bool SteinhartHartModelThermistor::isValidA(double a) {
    return std::isfinite(a);
  }
  double SteinhartHartModelThermistor::getA(void) const {
    return this->a;
  }

  bool SteinhartHartModelThermistor::isValidB(double b) {
    return std::isfinite(b);
  }
  double SteinhartHartModelThermistor::getB(void) const {
    return this->b;
  }

  bool SteinhartHartModelThermistor::isValidC(double c) {
    return std::isfinite(c);
  }
  double SteinhartHartModelThermistor::getC(void) const {
    return this->c;
  }

  bool SteinhartHartModelThermistor::isValidParameterIndex(unsigned int pi) const
  {
    return 0 <= pi && pi < 3;
  }

  double SteinhartHartModelThermistor::getParameterValue(unsigned int pi) const
  {
    if (! isValidParameterIndex(pi)) {
      throw std::invalid_argument("The given parameter index is invalid.");
    }

    switch (pi) {
    case 0:
      return getA();
    case 1:
      return getB();
    case 2:
      return getC();
    default:
      throw std::runtime_error("Default switch case should not be reachable.");
    }
  }

  bool SteinhartHartModelThermistor::isValidResistance(Resistance r) const {
    return isValidResistance(r);
  }
  Temperature SteinhartHartModelThermistor::getTemperature(Resistance r) const {
    double A = getA();
    double B = getB();
    double C = getC();
  
    double lnR = log(r);
    double Tinv = A + B * lnR + C * pow(lnR, 3);
    double T = 1.0/Tinv;

    return T;
  }


  bool SteinhartHartModelThermistor::isValidTemperature(Temperature t) const {
    return isValidKelvinTemperature(t);
  }
  Resistance SteinhartHartModelThermistor::getResistance(Temperature t) const {
    double A = getA();
    double B = getB();
    double C = getC();

    double y = (A - 1.0/t)/(2.0*C);
    double x = sqrt(pow(B/(3.0*C), 3) + pow(y, 2));
    double R = exp(pow(x-y, 1.0/3.0) - pow(x + y, 1.0/3.0));
    return R;
  }
 
  void SteinhartHartModelThermistor::accept(ThermistorVisitor* visitor) const
  {
    //visitor->visit(this);
  }

  std::ostream &SteinhartHartModelThermistor::print(std::ostream &stream) const {
    return stream << "Thermistor with Steinhart-Hart parameters:" <<
      " A=" << boost::lexical_cast<std::string>(getA()) <<
      " B=" << boost::lexical_cast<std::string>(getB()) <<
      " C=" << boost::lexical_cast<std::string>(getC());
  }

}

